I want to filter the documents that satisfy
(title = searchWord OR content = searchWord) AND (category = category OR category = category).
And I want to combine two OR statements, which are option 1 and 2 below, and I tried to do, but it doesn't work! Please help me to rectify it.
I want to keep the form of option 1 and option 2, and combine the two OR statements with AND .
//option 1
query.$or = [{ title: searchWord }, { content: searchWord }];
//option 2
query.$or = [{ category: category }, { category: category }];
//It doesn't work!!
query.$and = [{$or : [{ title: searchWord }, { content: searchWord }]},
{$or : [{ category: category }, { category: category }]}];
This is the mongodb query which can be of use to you. It uses an implicit and. Make sure to replace your collectionName.
db.collectionName.find({
$or : [
{"title": searchWord},
{"content": searchWord}
],
"category": category
})